package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.cart.service.CartInfoSyncService;
import com.atguigu.gmall.model.cart.CartInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Description: 加入购物车
 * @Author: Mr.Cao
 * @DateTime: 2021/10/11 20:03
 * BMW F750GS
 **/
@Service
public class CartInfoSyncServiceImpl extends ServiceImpl<CartInfoMapper,CartInfo> implements CartInfoSyncService {
    @Autowired
    private CartInfoMapper cartInfoMapper;
    //异步保存DB数据
    @Override
    @Async
    public void insertCartInfo(CartInfo cartInfo) {
        System.out.println("子线程异步保存DB数据：" + Thread.currentThread().getName());

        cartInfoMapper.insert(cartInfo);
    }

    @Override
    @Async
    public void updateCartInfo(CartInfo cartInfo) {
        System.out.println("子线程异步保存DB数据：" + Thread.currentThread().getName());
        //先从数据库中查
        CartInfo cartInfo1 = cartInfoMapper.selectOne(new QueryWrapper<CartInfo>().eq("sku_id", cartInfo.getSkuId()));
        if (cartInfo1!=null){
            //说明数据库中有, 则执行更新
            cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
                    .eq("user_id",cartInfo.getUserId())
                    .eq("sku_id",cartInfo.getSkuId()));
        }else {
            //没有,则执行添加操作
            cartInfoMapper.insert(cartInfo);
        }


    }
}
